//
//  HXDetailViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/29.
//

#import "HXDetailViewController.h"

@interface HXDetailViewController ()
@property (assign, nonatomic) int shishunindx;
@property (assign, nonatomic) int shiniindx;

@property (assign, nonatomic) int fenshunindx;
@property (assign, nonatomic) int fenniindx;
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙
@end

@implementation HXDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    self.shishunindx = 0;
    self.shiniindx = 0;
    self.fenshunindx = 0;
    self.fenniindx = 0;
    self.fd_prefersNavigationBarHidden = YES;
    self.blueToothManager = [BlueToothManager shareInstance];
    [self BottomFunction];
}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
    if ([themeColors isEqualToString:@"default"]) {
        mainscreen.image = [UIImage imageNamed:@"detailmian"];//detailmid
    }else{
        mainscreen.image = [UIImage imageNamed:@"detailmid"];//detailmid
    }
    
    [tabr addSubview:mainscreen];
    
    
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];
    
//    _linklogo.image = [UIImage imageNamed:[NSString stringWithFormat:@"linklogo%d",_dissindex]];//设备链接标识
    UIImageView *linklogo = [[UIImageView alloc] init];
    linklogo.image = [UIImage imageNamed:[NSString stringWithFormat:@"linklogo%ld",(long)self.index]];//设备链接标识
    [self.view addSubview:linklogo];
    [linklogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(30);
        make.left.mas_equalTo(86);
        make.width.mas_equalTo(29);
        make.height.mas_equalTo(36);
    }];
//    :CGRectMake(88, 40, 36, 36)
    UIImageView *deviceiconview = [[UIImageView alloc] init];
    deviceiconview.image = [UIImage imageNamed:@"deviceicon"];
    [self.view addSubview:deviceiconview];
    [deviceiconview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(30);
        make.left.mas_equalTo(linklogo.mas_right).offset(28);
        make.width.mas_equalTo(36);
        make.height.mas_equalTo(36);
    }];
    UILabel *name =[[UILabel alloc] init];
    if(!XHbluetoolUtils.beginurl){
        NSLog(@"self.isopen is no ");
        name.text = @"magic";
        
    }else{
        name.text = FC_SafeString(name.text = self.model.kCBAdvDataLocalName) ;
    }
//    name.text = self.model.kCBAdvDataLocalName;
    name.font = [UIFont systemFontOfSize:18];
    name.textColor = [UIColor whiteColor];
    [self.view addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(deviceiconview.mas_right).offset(8);
        make.centerY.mas_equalTo(deviceiconview.mas_centerY);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(180);

    }];
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];
    
    
    
    UIImageView *bjview = [[UIImageView alloc] init];
    if ([themeColors isEqualToString:@"default"]) {
        bjview.image = [UIImage imageNamed:@"detailbj"];
    }else{
        bjview.backgroundColor = midnightRGB;
        bjview.layer.cornerRadius = 8;
        bjview.layer.masksToBounds = YES;
    }
    
    [self.view addSubview:bjview];
    [bjview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(95.67);
        make.width.mas_equalTo(314.33);
        make.height.mas_equalTo(117.33);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UILabel *namelb = [[UILabel alloc] init];
    namelb.font = [UIFont systemFontOfSize:26];
    namelb.textColor = [UIColor whiteColor];
    namelb.text = @"Wall Paper";
    [bjview addSubview:namelb];
    [namelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20);
        make.height.mas_equalTo(21);
    }];
    
    UILabel *explainlb = [[UILabel alloc] init];
    explainlb.font = [UIFont systemFontOfSize:17];
    explainlb.textColor = [UIColor whiteColor];
    explainlb.numberOfLines = 2;
    explainlb.text = @"In solo mode code screen";
    [bjview addSubview:explainlb];
    [explainlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(namelb.mas_bottom).offset(3);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(53);
    }];
    
    UIImageView *zhutiview = [[UIImageView alloc] init];
    zhutiview.backgroundColor = [UIColor whiteColor];
    [bjview addSubview:zhutiview];
    [zhutiview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12.67);
        make.right.mas_equalTo(-47);
        make.bottom.mas_equalTo(-12.67);
        make.width.mas_equalTo(42);
    }];
//    detailbjt
    UIImageView *detailbjt = [[UIImageView alloc] init];
//    detailbjt.image = [UIImage imageNamed:@"detailbjt"];
    if ([themeColors isEqualToString:@"default"]) {
        detailbjt.image = [UIImage imageNamed:@"detailbjt"];
    }else{
        detailbjt.backgroundColor = midnightRGB;
        detailbjt.layer.cornerRadius = 8;
        detailbjt.layer.masksToBounds = YES;
    }
    [self.view addSubview:detailbjt];
    [detailbjt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bjview.mas_bottom).offset(16);
        make.width.mas_equalTo(314.33);
        make.height.mas_equalTo(90);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UILabel *statusbarlb = [[UILabel alloc] init];
    statusbarlb.font = [UIFont systemFontOfSize:25.56];
    statusbarlb.textColor = [UIColor whiteColor];
    statusbarlb.text = @"Statusbar Color";
    [detailbjt addSubview:statusbarlb];
    [statusbarlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(14);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(195);
    }];
    
    UIImageView *statusbarcolor = [[UIImageView alloc] init];
    statusbarcolor.backgroundColor = [UIColor whiteColor];
    [detailbjt addSubview:statusbarcolor];
    [statusbarcolor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(14);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(43);
        make.height.mas_equalTo(19);
    }];
    
    UIImageView *link = [[UIImageView alloc] init];
    link.backgroundColor = [UIColor whiteColor];
    [detailbjt addSubview:link];
    [link mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(44.67);
        make.bottom.mas_equalTo(-44.67);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(0.66);
    }];
    
    
    UILabel *staskbarlb = [[UILabel alloc] init];
    staskbarlb.font = [UIFont systemFontOfSize:25.56];
    staskbarlb.textColor = [UIColor whiteColor];
    staskbarlb.text = @"Taskbar Color";
    [detailbjt addSubview:staskbarlb];
    [staskbarlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(link.mas_bottom).offset(13);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(195);
    }];
    
    UIImageView *taskbarcolor = [[UIImageView alloc] init];
    taskbarcolor.backgroundColor = [UIColor whiteColor];
    [detailbjt addSubview:taskbarcolor];
    [taskbarcolor mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(link.mas_bottom).offset(13);
        make.right.mas_equalTo(-15);
        make.width.mas_equalTo(43);
        make.height.mas_equalTo(19);
    }];
    
//    hoursystembj
    UIImageView *hoursystembj = [[UIImageView alloc] init];
//    hoursystembj.image = [UIImage imageNamed:@"hoursystembj"];
    if ([themeColors isEqualToString:@"default"]) {
        hoursystembj.image = [UIImage imageNamed:@"hoursystembj"];
    }else{
        hoursystembj.backgroundColor = midnightRGB;
        hoursystembj.layer.cornerRadius = 8;
        hoursystembj.layer.masksToBounds = YES;
    }
    [self.view addSubview:hoursystembj];
    [hoursystembj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(detailbjt.mas_bottom).offset(16);
        make.width.mas_equalTo(314.33);
        make.height.mas_equalTo(46.33);
        make.centerX.mas_equalTo(self.view);
    }];
    
    
    UILabel *hoursystemlb = [[UILabel alloc] init];
    hoursystemlb.font = [UIFont systemFontOfSize:25];
    hoursystemlb.textColor = [UIColor whiteColor];
    hoursystemlb.text = @"Hour System";
    [hoursystembj addSubview:hoursystemlb];
    [hoursystemlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(8);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(150);
    }];
    
    UILabel *hourlb = [[UILabel alloc] init];
    hourlb.font = [UIFont systemFontOfSize:25];
    hourlb.textColor = [UIColor whiteColor];
    hourlb.textAlignment = NSTextAlignmentRight;
    hourlb.text = @"24-Hour";
    [hoursystembj addSubview:hourlb];
    [hourlb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-18);
        make.top.mas_equalTo(8);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(100);
    }];
    
    
//    interlockbj
    UIImageView *interlockbj = [[UIImageView alloc] init];
//    interlockbj.image = [UIImage imageNamed:@"interlockbj"];
    if ([themeColors isEqualToString:@"default"]) {
        interlockbj.image = [UIImage imageNamed:@"interlockbj"];
    }else{
        interlockbj.backgroundColor = midnightRGB;
        interlockbj.layer.cornerRadius = 8;
        interlockbj.layer.masksToBounds = YES;
    }
    [self.view addSubview:interlockbj];
    [interlockbj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hoursystembj.mas_bottom).offset(16);
        make.width.mas_equalTo(314.33);
        make.height.mas_equalTo(90.33);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UILabel *interlocklb = [[UILabel alloc] init];
    interlocklb.font = [UIFont systemFontOfSize:25];
    interlocklb.textColor = [UIColor whiteColor];
    interlocklb.text = @"Interlock";
    [interlockbj addSubview:interlocklb];
    [interlocklb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(120);
    }];
    
//    hoursystembj
    UIImageView *calibratebj = [[UIImageView alloc] init];
//    hoursystembj.image = [UIImage imageNamed:@"hoursystembj"];
    if ([themeColors isEqualToString:@"default"]) {
        calibratebj.image = [UIImage imageNamed:@"hoursystembj"];
    }else{
        calibratebj.backgroundColor = midnightRGB;
        calibratebj.layer.cornerRadius = 8;
        calibratebj.layer.masksToBounds = YES;
    }
    calibratebj.userInteractionEnabled = YES;
    [self.view addSubview:calibratebj];
    [calibratebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(interlockbj.mas_bottom).offset(16);
        make.width.mas_equalTo(314.33);
        make.height.mas_equalTo(46.33);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UIButton *calibratebt = [UIButton buttonWithType:UIButtonTypeCustom];
    [calibratebt addTarget:self action:@selector(jumpHGalibration) forControlEvents:UIControlEventTouchUpInside];
    [calibratebj addSubview:calibratebt];
    [calibratebt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(calibratebj);
    }];
    
    UILabel *calibratelb = [[UILabel alloc] init];
    calibratelb.font = [UIFont systemFontOfSize:25];
    calibratelb.textColor = [UIColor whiteColor];
    calibratelb.textAlignment = NSTextAlignmentLeft;
    calibratelb.text = @"Calibrate the watch";
    [calibratebj addSubview:calibratelb];
    [calibratelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-43);
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(8);
        make.height.mas_equalTo(28);
    }];
    
    UIImageView *watchesview  = [[UIImageView alloc] init];
    watchesview.image = [UIImage imageNamed:@""];
    [calibratebj addSubview:watchesview];
    [watchesview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(13);
        make.width.height.mas_equalTo(20.33);
    }];
    
    
}
-(void)longPressGesture:(id)sender
{
    //监听长按的状态
    //    说明：长按手势的常用状态如下
    //   1 开始：UIGestureRecognizerStateBegan
    //   2 改变：UIGestureRecognizerStateChanged
    //   3 结束：UIGestureRecognizerStateEnded
    //   4 取消：UIGestureRecognizerStateCancelled
    //   5 失败：UIGestureRecognizerStateFailed
//    01：时针向左（不停 一直转）（校准模式使用）
//    02：时针向右（不停 一直转）（校准模式使用）
//    03：分针向左（不停 一直转）（校准模式使用）
//    04：分针向右（不停 一直转）（校准模式使用）
    UILongPressGestureRecognizer *lonPress = sender;
    NSLog(@"分逆");
    if (lonPress.state == UIGestureRecognizerStateBegan)
    {
             //your code
        NSLog(@"开始");
        Byte send[] = {0x58,0x57,0x03,0x03,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
             
        [self.blueToothManager writeData:sendData];
             
    }
    if(lonPress.state == UIGestureRecognizerStateEnded){
        
        Byte send[] = {0x58,0x57,0x03,0x00,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
        
    }
}


- (void)shishun{
//    时针向左走发送数据
//    (0x58,0x57,0x03,0x05,0x01,0x00,0x00,0x01)
//    (0x58,0x57,0x03,0x05,0x02,0x00,0x00,0x02)
//    (0x58,0x57,0x03,0x05,0x03,0x00,0x00,0x03)
//    (0x58,0x57,0x03,0x05,0x04,0x00,0x00,0x04)
//    01：时针向左（不停 一直转）（校准模式使用）
//    02：时针向右（不停 一直转）（校准模式使用）
//    03：分针向左（不停 一直转）（校准模式使用）
//    04：分针向右（不停 一直转）（校准模式使用）
//    05：时针向左走一步（字节4需要一直累加到0xff后归零）（校准模式使用）
//    06：时针向右走一步（字节4需要一直累加到0xff后归零）（校准模式使用）
//    07：分针向左走一步（字节4需要一直累加到0xff后归零）（校准模式使用）
//    08：分针向右走一步（字节4需要一直累加到0xff后归零）（校准模式使用）
//    09：正常走时模式（转为正常走时模式）
//    10：标定模式
//    11：获取数据
//    12：直接进入睡眠（调试备用）
//    13：调节走时速度（调试备用）
//    14：调节走时速度（调试备用）
//    15：发送走时时间（设置分钟，时钟）
//    16:    获取数据
//    17:    获取数据
//    255（0xff）：设置走时时间（设置分钟，时钟）
    NSLog(@"时顺");
    self.shishunindx ++;
    self.shishunindx = [XHbluetoolUtils intTransitionhex16:self.shishunindx];
    Byte send[] = {0x58,0x57,0x03,0x06,self.shishunindx,0x00,0x00,self.shishunindx};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

    [self.blueToothManager writeData:sendData];
    
}
- (void)shini{
    
    NSLog(@"时逆");
    self.shiniindx ++;
    self.shiniindx = [XHbluetoolUtils intTransitionhex16:self.shiniindx];
    Byte send[] = {0x58,0x57,0x03,0x05,self.shiniindx,0x00,0x00,self.shiniindx};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

    [self.blueToothManager writeData:sendData];
}
-(void)fenshu{
    NSLog(@"分顺");
    self.fenshunindx ++;
    self.fenshunindx = [XHbluetoolUtils intTransitionhex16:self.fenshunindx];
    Byte send[] = {0x58,0x57,0x03,0x08,self.fenshunindx,0x00,0x00,self.fenshunindx};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

    [self.blueToothManager writeData:sendData];
}
-(void)fenni{
   /* NSLog(@"分逆");
    self.fenniindx ++;
    self.fenniindx = [XHbluetoolUtils intTransitionhex16:self.fenniindx];
    Byte send[] = {0x58,0x57,0x03,0x07,self.fenniindx,0x00,0x00,self.fenniindx};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

    [self.blueToothManager writeData:sendData];
    */
    [self jumpHGalibration];
}
-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)jumpHGalibration{
    
    HXGalibrationViewController*jum = [[HXGalibrationViewController alloc] init];
    jum.modalPresentationStyle = NSDateFormatterNoStyle;
//    [self presentViewController:jum animated:YES completion:nil];
    [self.navigationController pushViewController:jum animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
